package me.muci.A_base.processingFlow;

import org.junit.Test;

import java.io.*;
import java.util.Objects;

/**
 * ClassName: ObjectInputAndOutputStreamTest
 * <p>
 * Description: 对象流的使用，ObjectInputStream  ObjectOutputStream
 * <p>
 * date: 2021/8/18 10:29
 *
 * @author muci
 * @since JDK 1.8
 */
public class ObjectInputAndOutputStreamTest {

    @Test
    public void ObjectOutputStreamTest(){
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("src\\main\\resources\\objectStream\\ObjectOutputStreamTest.dat"));
            oos.writeObject(new String("hello，沐柌"));
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void objectInputStreamTest(){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("src\\main\\resources\\objectStream\\ObjectOutputStreamTest.dat"));
            Object obj = ois.readObject();
            System.out.println(obj.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (!Objects.isNull(ois)){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
